package servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ejb.AccountBeanLocal;
import ejb.ClientRepositoryBean;
import ejb.LoginBeanLocal;
import entities.Client;

/**
 * Servlet implementation class RegistrationChild
 */
@WebServlet("/RegistrationChild")
public class RegistrationChild extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	AccountBeanLocal account;
	@EJB
	LoginBeanLocal lb;
	/**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrationChild() {
        super();
        // TODO Auto-generated constructor stub
    }

    
     private void processrequest(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException
     {
     	HttpSession session = request.getSession();
     	
    	Client masterClient = lb.getUser((String)session.getAttribute("Username"));
    	Client cl = new Client();
 		String lastName = new String( request.getParameter("lastName").getBytes("ISO-8859-1"),"UTF-8");
 		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
 		String secondName = new String( request.getParameter("secondName").getBytes("ISO-8859-1"),"UTF-8");
 		String gender = new String(request.getParameter("gender").getBytes("ISO-8859-1"),"UTF-8");
 		String pasport = new String (request.getParameter("pasport").getBytes("ISO-8859-1"),"UTF-8");
 		cl.setName(masterClient.getName()+"Child");
 		cl.setPatronymic(secondName);
 		cl.setFirstname(lastName);
 		cl.setSecondname(name);
 		cl.setGender(gender);
 		cl.setPassport(pasport);
 		cl.setisRegistered("false");
 		cl.setMasterClient(masterClient);
 		lb.addUser(cl);
 		response.sendRedirect("/NCProjectWeb/CheckLoginBeforeBy?id="+request.getParameter("id"));

 		}
    	 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processrequest(request,response);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processrequest(request,response);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
